package com.hqyj.config;

import com.hqyj.callback.ShopMqConfirm;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * 项目:rabbitmq-230301-provider
 * 描述:
 * 时间:2023/8/4 15:15
 * 作者:admin
 * 版本:1.0
 **/
@Configuration
public class ShopMqConfig {

    /**
     * 消息转换器
     * @return
     */
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    /**
     * 商品秒杀队列
     * @return
     */
    @Bean
    public Queue shopQueue()
    {
        return  new Queue("shopQueue",true,false,false);
    }

    /**
     * 商品秒杀队列的交换机
     * @return
     */
    @Bean
    public DirectExchange shopExchange(){
        return  new DirectExchange("shopExchange",true,false);
    }

    /**
     * 绑定商品秒杀队列
     * @return
     */
    @Bean
    public Binding bingExchange(){
        return BindingBuilder.bind(shopQueue()).to(shopExchange()).with("shopRoute");
    }

    @Autowired
    ShopMqConfirm shopMqConfirm;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @PostConstruct
    public void init(){
        rabbitTemplate.setConfirmCallback(shopMqConfirm);
   }
}
